<template>
  <div>
    <el-table :data="PermissionLArr" style="width: 100%" row-key="id" border>
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="权限值" prop="code"></el-table-column>
      <el-table-column label="修改时间" prop="updateTime"></el-table-column>
      <el-table-column label="操作" width="width">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            v-if="row.level != 4"
            @click="addPermission(row)"
          >
            {{ row.level == 3 ? '添加功能' : '添加菜单' }}
          </el-button>
          <el-button
            type="primary"
            :disabled="row.level == 1"
            @click="updatePermission(row)"
          >
            编辑
          </el-button>

          <el-popconfirm
            :title="`确认删除${row.name}吗？`"
            @confirm="removeMenu(row.id)"
          >
            <template #reference>
              <el-button type="primary" :disabled="row.level == 1">
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框组件：添加或更新已有的菜单的数据结构 -->
    <el-dialog
      :title="menuData.id ? '更新菜单' : '新增菜单'"
      v-model="dialogVisible"
    >
      <el-form ref="form">
        <el-form-item label="名称">
          <el-input
            placeholder="请输入菜单名称"
            v-model="menuData.name"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限值">
          <el-input
            placeholder="请输入菜单权限值"
            v-model="menuData.code"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  reqAllPermission,
  reqAddOrUpdateMenu,
  reqRemoveMenu,
} from '@/api/acl/menu'
import type {
  ResponseData,
  Permission,
  Permissions,
  PermissionResponseData,
  MenuParams,
} from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'

// 存储菜单的数组
const PermissionLArr = ref<Permissions>([])
// 控制对话框的显示与隐藏
let dialogVisible = ref<boolean>(false)
// 携带的参数
let menuData = reactive<MenuParams>({
  code: '',
  level: 0,
  name: '',
  pid: 0,
})

// 获取菜单数据的方法
const getHasPermission = async () => {
  let result: PermissionResponseData = await reqAllPermission()
  if (result.code == 200) {
    PermissionLArr.value = result.data
  }
}
// 添加菜单按钮的回调
const addPermission = (row: Permission) => {
  // 清空数据
  Object.assign(menuData, {
    code: '',
    level: 0,
    name: '',
    pid: 0,
  })
  dialogVisible.value = true
  // 收集新增的菜单的level值
  menuData.level = row.level + 1
  // 给谁新增子菜单
  menuData.pid = row.id
}
// 修改菜单按钮的回调
const updatePermission = (row: Permission) => {
  dialogVisible.value = true
  Object.assign(menuData, row)
}
// 对话框确定按钮的回调
const save = async () => {
  let result: ResponseData = await reqAddOrUpdateMenu(menuData)
  if ((result.code = 200)) {
    dialogVisible.value = false
    ElMessage({
      type: 'success',
      message: menuData.id ? '修改成功' : '添加成功',
    })
    getHasPermission()
  }
}
onMounted(() => {
  getHasPermission()
})

// 删除按钮的回调
const removeMenu = async (id: number) => {
  let result = await reqRemoveMenu(id)
  if (result.code == 200) {
    if (result.code == 200) {
      ElMessage({ type: 'success', message: '删除成功' })
      getHasPermission()
    }
  }
}
</script>

<style scoped></style>
